/**
 * Created by Administrator on 14-8-21.
 */
var debitCardArray = new Array();
var creditCardArray = new Array();
var changExCardFlag = false;
$(function(){
    $.ajaxSetup({cache:false});

   //$("#bankCardNo").keyup(processValueChange);
    //将keyup改为 文本改变即触发事件。 update  by dongdh  20160325
    $(document).on('input propertychange', '#bankCardNo', processValueChange);
    
    expressSelInit();

    $(':button').removeAttr("disabled");
});

//卡号文本框输入 值改变事件函数
function processValueChange() {

    $("#notSupportTip").hide();
    $("#freezeTip").hide();

    var num = /^\d*$/;  //全数字
    var str = this.value;
    var laststr = str.charAt(str.length - 1);
    if (!num.exec(laststr)) {
        this.value=this.value.substr(0,str.length - 1);
        return false;
    }
    this.value =this.value.replace(/\s/g,'').replace(/(\d{4})(?=\d)/g,"$1 ");
    avalPayCheckInit();
    var bankCardNo = this.value.replace(/\s+/g, "");
    if($.trim(bankCardNo).length >=10){
        checkCardNo(bankCardNo);
        changExCardFlag = true;
    }else{
        $("#payBankTr").hide();
        $("#exPayChannel").val("");
        $("#exPayBankCode").val("");
    }

}



function expressSelInit(){
    if($("#exCardId_1").length > 0){
        var epc = $("#exPayChannel1").val();
        var epb = $("#exPayBankCode1").val();
        var bcn = $("#bankCardNo1").val();
    /*    if($("#firstExpressView").length >0 && $("#firstExpressView").css("display") != "none" ){
            //更换虚拟帐号数据初始化问题
            bcn = "";
        }*/
        reUseBindCard();
        exBankSel("1",epc,epb,bcn);
    }
}

function checkSms(formId){
    var formData = $("#"+formId+"").getFormData();
    
    var res = false;
    $("#exSmsCodeTip").hide();
    jQuery.ajax({
        url :ctx+"/00100/express/checkSms.shtml?t="+(new Date()).getTime(),
        async:false,
        dataType:"json",
        data:formData,
        success: function (data) {
            if(data.result && data.result == "1"){
                res = true;
            }else{
                var failCount = data.failCount;
                if(failCount <5){
                    $("#exSmsCodeTip").html("短信验证码错误，您还有"+(5-failCount)+"次机会").show();
                }else if(failCount >=5){
                    $("#exSmsCodeTip").html("您当日的短信验证码错误次数超限，该卡已被冻结").show();
                }
            }
        }
    });
    if(!res)
        $("#exSmsCodeTip").show();
    return res;
}


function sendSms(sendSmsBtn,formId){
    $("#exSmsCodeTip").hide();
    var obj = $("#"+sendSmsBtn+"");
    obj.attr('disabled','disabled');
    
    var formData = $("#"+formId+"").getFormData();
    
    //处理隐藏字段
  //处理星号的字段 exholder  和    idcard
    var exHolder = $.trim($('#exHolder').attr('initData'));
    if (exHolder != '') {
    	formData.exHolder = exHolder;
    }
    
    var exIdCardNo = $.trim($('#exIdCardNo').attr('initData'));
    if (exIdCardNo != '') {
    	formData.exIdCardNo = exIdCardNo;
    }
    
    $.post(ctx+"/00100/express/getSms.shtml", formData, function(data) {
        if(data.result && data.result == "1"){
            var max = 60;
            $(obj).attr('disabled','disabled');
            $(obj).attr('count',max);
            $(obj).prop("class","tips-6 zoom verticalTop").text('重新获取('+max+'s)');
            setTimeout('checkTimeRemain("'+sendSmsBtn+'")',1000);
        }else{
            $(obj).removeAttr('disabled');
            var msg = "获取短信验证码失败";
            if(data.errMsg =="1"){
                msg = "短信验证码发送次数超限，请从商户网站重新下单";
            }else if(data.errMsg =="2"){
                msg = "银行卡信息有误，验证码发送失败";
            }
            $("#exSmsCodeTip").html("").html(msg).show();
        }
    },'json');
}
function checkTimeRemain(objId){
    var obj = document.getElementById(objId);
    var c = new Number($(obj).attr('count'));
    var next = c-1;
    if(c <= 0){
        $(obj).removeAttr('disabled');
        $(obj).text($(obj).attr('title'));
        return;
    }else{
        $(obj).attr('count',next);
        if(next!=0){
            $(obj).prop("class","tips-6 zoom verticalTop").text('重新获取('+next+'s)');
        }else{
            $(obj).prop("class","tips-5 zoom verticalTop").text('免费获取');
        }
        setTimeout('checkTimeRemain("'+objId+'")',1000);
    }
}


function checkCardNo(ccardNo){
    $("#freezeTip").hide();
    ccardNo = ccardNo.replace(/\s+/g, "");
    if(!(/^\d{10,25}/.test(ccardNo))){
        return;
    }
    $.get(ctx+"/00100/express/cbin.shtml", {ccardNo: ccardNo}, function(data) {
    	
    	//add by dongdh 20160330
    	$("#expressPayBankImg").attr("src", '');
        $("#expressCardTypeTip").removeClass("zoom").removeClass("btn-1").html("");
    	
        if(data.cbin == "0"){//未知
            $("#payBankTr").show();
            $("#binTd").hide();
            $("#noBinTd").show();
            $("#exPayChannel").val("");
            $("#exPayBankCode").val("");
        }else{
            var bankCode = data.bankCode;
            var sysSupport = false;

            $("#binTd").show();
            $("#noBinTd").hide();

            //识别出但系统不支持
            if(data.cbin == "1" && hasContains(debitCardArray,bankCode)){
                $("#expressPayBankImg").attr("src",ctx+"/BankIcon/"+bankCode+".jpeg");
                $("#expressCardTypeTip").prop("class","btn-3 zoom").html("储蓄卡");
                $("#exPayChannel").val("03");
                $("#exPayBankCode").val(bankCode);
                sysSupport = true;
            }else if(data.cbin == "2" && hasContains(creditCardArray,bankCode)){
                $("#expressPayBankImg").attr("src",ctx+"/BankIcon/"+bankCode+".jpeg");
                $("#expressCardTypeTip").prop("class","btn-1 zoom").html("信用卡");
                $("#exPayChannel").val("06");
                $("#exPayBankCode").val(bankCode);
                sysSupport = true;
            }
            if(!sysSupport){
                $("#notSupportTip").show();
                
                //add by dondh 20160330
                var src = $('#binTd #expressPayBankImg').attr('src');
        		if (!src || src == '') {
        			$("#payBankTr").hide();
        		}

            }else{
                $("#payBankTr").show();
            }
        }
    },"json");
}

//下一步
var expressLocationSubmit = function(loginFlag,btnId){
    if($("#notSupportTip").css("display") != "none"){
        return false;
    }
    if(((changExCardFlag && loginFlag =="1" || (loginFlag =="0")) && $("#exPayWay").val()!="A" && $("#bankCardNo").val()=="")){
        $("#freezeTip").html("银行卡号输入错误").show();
        return false;
    }
    if(($("#exPayChannel").val()=="" || $("#exPayBankCode").val()=="") && $("#exPayWay").val()!="A"){
        if(loginFlag == "1") {
        	
        	//add by dongdh  20160330
    		var src = $('#binTd #expressPayBankImg').attr('src');
    		if (!src || src == '') {
    			$('#binTd').hide();
    			$('#noBinTd').show();
    		}
        	
            $("#payBankTr").show();
        }
        if($("#exCardId").length == 0 && $("#payBankTr").css("display") == "none"){
            $("#freezeTip").html("银行卡号输入错误").show();
            return false;
        }
        $("#selectPayBankTip1").show();
        $("#selectPayBankTip2").show();
        return false;
    }

    if(loginFlag == "1" && $("#exPayWay").val() == "A"){//余额支付
        $("#"+btnId).attr("disabled","disabled");
        $("#locationBankFrom").submit();
        $("#"+btnId).removeAttr("disabled");
    }else{
        var ccardNo = $("#bankCardNo").val().replace(/\s+/g, "");
        if($.trim(ccardNo).length < 15){
            $("#freezeTip").html("银行卡号不能小于15位。").show();
            return false;
        }
        if($.trim(ccardNo).length > 25){
            $("#freezeTip").html("银行卡号不能大于25位。").show();
            return false;
        }
        if(changExCardFlag) {
            $("#changExCardFlag").val("0");
        }else{
            $("#changExCardFlag").val("1");
        }

        $("#"+btnId).attr("disabled","disabled");
        $.get(ctx+"/00100/express/status.shtml", {ccardNo: ccardNo}, function(data) {
            $("#"+btnId).removeAttr("disabled");
            if(data.sta == "1"){
                $("#freezeTip").html("该卡号已被锁定，请更换其他银行卡。").show();
                $("#selectPayBankTip1").show();
                $("#selectPayBankTip11").html("该卡号已被锁定，请更换其他银行卡。1");
                return false;
            }if(data.sta == "2"){
                $("#freezeTip").html("卡号不能为空。").show();
                $("#selectPayBankTip1").show();
                $("#selectPayBankTip11").html("卡号不能为空。");
                return false;
            }else{
                $("#locationBankFrom").submit();
            }
        },"json");
    }
};

var expressSwitchGopay = function(){
    if($("#gopayChannelSel").length>0){
        $(".tabsUl .tabsLabel").removeClass("cur");
        $("#gopayChannelSel").addClass("cur");
        showChannel("06");
    }
}

var hasContains = function(array,value){
    return jQuery.inArray(value,array) != -1;
}

var changeExCard = function () {
    $("#exCardListDiv").hide();
    $("#firstExpressView").show();
    $("#payBankTr").hide();
    changExCardFlag = true;
    //删除已选中
    $('input[name="exCardId"]:checked').removeAttr("checked");
    $("#exPayChannel").val("F");
    
    if (bankAcctNum != '') {
    	 $('#bankCardNo').val(bankAcctNum)
    	$('#bankCardNo').trigger('input');
    	$('#bankCardNo').trigger('propertychange');
    } else {
    	$("#exPayBankCode").val("");
    	$("#bankCardNo").val("");
    }
    avalPayCheckInit();
}

var exBankSel = function (index,exPayChannel,exPayBankCode,bankCardNo) {
    $("#selectPayBankTip1").hide();
    $("#selectPayBankTip2").hide();
    $("#exCardId_"+index+"").prop("checked", true);
    $("#exPayChannel").val(exPayChannel);
    $("#exPayBankCode").val(exPayBankCode);
    $("#bankCardNo").val(bankCardNo);
    avalPayCheckInit();
}

function avalPayCheckInit(){

    if($("#yesAvalPay").length>0) {
        $("#yesAvalPay").removeAttr("checked");
    }
    if($('input:checkbox[name="noAvalPay"]').is(":checked")){
        $("#payWay").val("S");
        $("#exPayWay").val("S");
    }else{
        $("#payWay").val("F");
        $("#exPayWay").val("F");
    }
}

//登录支付确认---------------------
function exPayConfirmSendSms(sendSmsTip,reSendSmsTip,form){
    if(commonPayCheckNoSms()){
        sendSms(sendSmsTip,reSendSmsTip,form);
    }
}

function exGopayConfirmSubmit(formId){
    $("#confirmSubmit").attr("disabled","disabled");
    //if (expressGopayPwdCtrl){
    //    $.get(ctx+"/getPayPwdUUID.shtml",{ts:new Date().getTime(),count:1,safe:1},function(r){
    //        if(r&&r.code1){
    //            expressGopayPwdCtrl.encrypt(r.code1,1);
    //            $("#"+formId+"").submit();
    //        }else{
    //            $.alert("获取支付密码随机码失败");
    //        }
    //    },"json");
    //}else{
    //    $("#"+formId+"").submit();
    //}
    if ($("#exCardId").val()!='' ||  $("#exPayWay").val()=='A'){
        if(!microdoneTip("expressGopayPwdCtrlTip")){
            $("#confirmSubmit").removeAttr("disabled");
            return false;
        }
    }
    $("#"+formId+"").submit();
}

function exConfirmSubmit(formId,exPayChannel){
    var check = false;
    if($("#exCardId").val() == ""){
        if($("#checkAgreeOpen").length>0 && $("#checkAgreeOpen").val() == "1"){
            check = commonPayCheck() && checkAgreeOpen()&& checkSms(formId);
        }else{
            check = commonPayCheck() && checkSms(formId);
        }
    }else{
        if(exPayChannel == "06"){
            check = checkExCvn2();
        }else{
            check = checkExIdCardNo6();
        }
    }
    if(check){
        exGopayConfirmSubmit(formId);
    }
}
//---------------

//-------------首次支付--------------
function firstPayCheck(btnId,formId){
    if(commonPayCheck() && checkAgreeOpen() && checkSms(formId)){
        $("#"+btnId).attr("disabled","disabled");
        
        //处理星号的字段 exholder  和    idcard
        var exHolder = $.trim($('#exHolder').attr('initData'));
        if (exHolder != '') {
        	$('#exHolder').attr('name', 'exHolder_temp');
        	$("#"+formId+"").append("<input type='hidden'  name='exHolder' value= '" + exHolder + "'>");
        }
        
        var exIdCardNo = $.trim($('#exIdCardNo').attr('initData'));
        if (exIdCardNo != '') {
        	$('#exIdCardNo').attr('name', 'exIdCardNo_temp');
        	$("#"+formId+"").append("<input type='hidden' name='exIdCardNo' value= '" + exIdCardNo + "'>");
        }
        //alert($("#"+formId+"").html());
        $("#"+formId+"").submit();
    }
}

function firstSendSms(sendSmsTip,reSendSmsTip,form){
    if(commonPayCheckNoSms()){
        sendSms(sendSmsTip,reSendSmsTip,form);
    }
}
//---------------

function reUseBindCard(){
    changExCardFlag = false;
    $("#exCardListDiv").show();
    $("#firstExpressView").hide();
}